#include "Mainpage.h"
#include "ui_Mainpage.h"
#include "Client.h"
#include <QMessageBox>
#include <QHostAddress>
#include <QBitmap>
#include <QStyleOption>
#include "mymessagebox.h"

Mainpage::Mainpage(const QString &id, Client *client, QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Mainpage)
    , clientWindow(client)
    , employeeId(id)  // 通过构造函数传递工号
{
    ui->setupUi(this);

    // 隐藏原生标题栏
    setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);

    // 防止窗口背景透明
    setAttribute(Qt::WA_TranslucentBackground, false);

    // 点击关闭按钮关闭窗口
    connect(ui->returnButton_2, &QPushButton::clicked, this, &Mainpage::close);

    ui->num->setText("工号: " + employeeId + " 欢迎你" );

    // 初始化 TCP 连接
    tcpSocket = new QTcpSocket(this);
    tcpSocket->connectToHost(QHostAddress("127.0.0.1"), 12350);

    connect(tcpSocket, &QTcpSocket::readyRead, this, [=]() {
        QByteArray response = tcpSocket->readAll();

        if (response == "TAG_0") {
            // QMessageBox::warning(this, "错误", "请先录入人脸");
            MyMessageBox msgBox;
            msgBox.setWindowTitle("错误");
            msgBox.setText("请先录入人脸");
            msgBox.setButton("确定");
            msgBox.exec();
        } else if (response == "VAC_OK") {
            Vacation *vacation = new Vacation(employeeId, this);
            vacation->show();
            this->hide();
        } else if (response == "RES_OK") {
            Resignature *resignature = new Resignature(employeeId, this);
            resignature->show();
            this->hide();
        } else if (response == "SIT_OK") {
            Situation *situation = new Situation(employeeId);
            situation->show();
        } else {
            // QMessageBox::warning(this, "错误", "服务器返回未知信息");
            MyMessageBox msgBox;
            msgBox.setWindowTitle("错误");
            msgBox.setText("服务器返回未知信息");
            msgBox.setButton("确定");
            msgBox.exec();
        }
    });
}

Mainpage::~Mainpage()
{
    delete ui;
}

// 实现窗口拖动功能
void Mainpage::mousePressEvent(QMouseEvent *event)
{
    // 检查是否点击了自定义标题栏
    if (event->button() == Qt::LeftButton && event->pos().y() <= 60) {
        m_isTitleBarClicked = true;
        m_dragPosition = event->globalPos() - frameGeometry().topLeft();
        event->accept();
    } else {
        m_isTitleBarClicked = false;
        // 让事件继续传播，确保其他控件能响应点击事件
        QWidget::mousePressEvent(event);
    }
}

void Mainpage::mouseMoveEvent(QMouseEvent *event)
{
    // 只有在点击了标题栏之后才允许拖动
    if (m_isTitleBarClicked && (event->buttons() & Qt::LeftButton)) {
        move(event->globalPos() - m_dragPosition);
        event->accept();
    } else {
        // 让事件继续传播，确保其他控件能响应移动事件
        QWidget::mouseMoveEvent(event);
    }
}

void Mainpage::paintEvent(QPaintEvent *event)
{
    // 1. 绘制样式（QSS）
    QStyleOption opt;
    opt.initFrom(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); // 绘制样式

    // 2. 设置掩码，约定 painter 绘制区域
    QBitmap bmp(this->size());
    bmp.fill(Qt::white); // 填充 bitmap

    QPainter painter(&bmp);
    painter.setPen(Qt::NoPen);
    painter.setBrush(Qt::black); // 掩码值
    painter.drawRoundedRect(bmp.rect(), 30, 30); // 在 bitmap 中绘制圆角矩形

    // 设置掩码以确定本控件可绘制区域
    setMask(bmp);

    // 调用基类的 paintEvent
    QWidget::paintEvent(event);
}

void Mainpage::on_returnButton_clicked()
{
    this->close();
    clientWindow->show();
}


void Mainpage::on_num_clicked()
{
    // 直接在构造函数中传递工号
    Myself *myself = new Myself(employeeId, this);
    myself->show();
    this->hide();
}


void Mainpage::on_resignature_clicked()
{
    // 发送 employeeId 给服务器
    if (tcpSocket->isOpen()) {
        QString request = "RESIG_TAG:" + employeeId;
        tcpSocket->write(request.toUtf8());
        tcpSocket->flush();
    } else {
        // QMessageBox::warning(this, "错误", "无法连接到服务器");
        MyMessageBox msgBox;
        msgBox.setWindowTitle("错误");
        msgBox.setText("无法连接到服务器");
        msgBox.setButton("确定");
        msgBox.exec();
    }
}


void Mainpage::on_vacation_clicked()
{
    // 发送 employeeId 给服务器
    if (tcpSocket->isOpen()) {
        QString request = "VACAT_TAG:" + employeeId;
        tcpSocket->write(request.toUtf8());
        tcpSocket->flush();
    } else {
        // QMessageBox::warning(this, "错误", "无法连接到服务器");
        MyMessageBox msgBox;
        msgBox.setWindowTitle("错误");
        msgBox.setText("无法连接到服务器");
        msgBox.setButton("确定");
        msgBox.exec();
    }
}


void Mainpage::on_face_clicked()
{
    // 直接在构造函数中传递工号
    Face *face = new Face(employeeId, this);
    face->show();
    this->hide();
}


void Mainpage::on_mail_clicked()
{
    Mail *mail = new Mail(employeeId);
    mail->show();
}


void Mainpage::on_situation_clicked()
{
    // 发送 employeeId 给服务器
    if (tcpSocket->isOpen()) {
        QString request = "SITUA_TAG:" + employeeId;
        tcpSocket->write(request.toUtf8());
        tcpSocket->flush();
    } else {
        // QMessageBox::warning(this, "错误", "无法连接到服务器");
        MyMessageBox msgBox;
        msgBox.setWindowTitle("错误");
        msgBox.setText("无法连接到服务器");
        msgBox.setButton("确定");
        msgBox.exec();
    }
}

